#ifndef CHARTVIEWEX_H
#define CHARTVIEWEX_H

#include "chartviewex_global.h"
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QAbstractSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QAbstractAxis>
#include <QtCharts/QChart>
#include <QWidget>
#include <QMenu>
#include <QList>
#include <QPixmap>
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QSpinBox>
#include <QComboBox>
#include <QCheckBox>
#include <QLineEdit>
#include <QGroupBox>
#include <QVBoxLayout>
#include "ccurvedata.h"
QT_CHARTS_USE_NAMESPACE

class CHARTVIEWEXSHARED_EXPORT ChartViewEx: public QChartView
{
    Q_OBJECT
public:
    ChartViewEx(QWidget *parent=0);
	void setChartBackgroundColor(QColor color);
	void setChartPlotAreaColor(QColor color);
	void setChartForegroundColor(QColor color);
	void setGridColor(QColor color);
	void setGridNumber(int xCount, int yCount);

	void setChartName(const QString &name);
    void setXAxisName(const QString &name);
	void setYAxisName(const QString &name);
	void setAxisReverse(bool xReverse = false, bool yReverse = false);
	void setAxisXDecimalNumber(QString format);
	void setAxisYDecimalNumber(QString format);
	void setXAxisRange(float minX, float maxX);
	void setYAxisRange(float minY, float maxY);

	void addCurveData(CCurveData *data);
	void addCurveData(QString name,QList<float> lstX, QList<float> lstY, QColor lineColor = Qt::black,int lineWidth = 2);
	void appendCurveData(QString name, QList<float> lstX, QList<float> lstY);
	void appendCurveData(int lineIndex, QList<float> lstX, QList<float> lstY);
	void deleteCurveData(int lineIndex);
	void deleteCurveData(QString name);
	void hideCurveData(int lineIndex);
	void hideCurveData(QString name);

    void saveImage();
    void ClearAll();
private:
    void connectMarkers();
    void createActions();
    void createDialog();
    void fillColorList(QComboBox *cbBox);
    void checkPointValid(QPointF &pos);
	int checkSeriesIsExistOrNot(QString name);
protected:
    void contextMenuEvent(QContextMenuEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *e);
    void paintEvent(QPaintEvent *event);
private slots:
    void setHoveredSigal(const QPointF &point, bool state);
    void showSetPropertyDialog();
    void legendVisible();
    void markerVisible();
    void handleMarkerClicked();
    void SelectAllNorNot();
	void hoveredEnable();
    void getLegendAlign(int index);
    void getLineType(int index);
    void getLineName(const QString &name);
    void getSpinBoxValue(int index);
	void getDoubleSpinBoxValue(double value);
    void getColor(int index);
	void setOptionEnable(bool enable);
private:
    QMenu *popMenu;
    QAction *setCurvePropertyAction;
    QAction *saveImgAction;
    QAction *legendVisibleAction;
    QAction *markerAction;
	QAction *hoveredEnableAction;
	QAction *selectAllCurve;
	QAction *hideAllCurve;

    QChart* mChart;
    QPointF posStart;
    QPointF posEnd;
    QPointF posCur;

    bool isPress;
    bool isHoverd;
    bool isLegendVisible;
    bool isMarkerVisible;
	bool isHoveredEnable;
    qreal mXMin;
    qreal mXMax;
    qreal mYMin;
    qreal mYMax;
    qreal mZoomFactor;

    QDialog *curveDialog;
    QLabel *lbLineName;
    QLabel *lbLineType;
    QLabel *lbLineWidth;
    QLabel *lbColor;
    QSpinBox *spinBoxLineWidth;
    //QPushButton *btnColor;
    QComboBox *cbBoxLineColor;
    QLineEdit *lELineName;
    QComboBox *cbBoxLineType;


    QDialog *chartDialog;
    QLabel *lbChartBackground;
	QLabel *lbChartForeground;
	QLabel *lbPlotAreaColor;

	QLabel *lbChartTitle;
	QLabel *lbLegendAlign;

	QLabel *lbXAixsName;
    QLabel *lbYAxisName;
	QLabel *lbXAxisDecimal;
	QLabel *lbYAxisDecimal;
	QLabel *lbXAxisMin;
	QLabel *lbXAxisMax;
	QLabel *lbYAxisMin;
	QLabel *lbYAxisMax;
	QLabel *lbXGridCount;
	QLabel *lbYGridCount;
	QLabel *lbGridColor;

    QLabel *lbZoomFactor;
    //QPushButton *btnBackgroundColor;
    QComboBox *cbBoxBkColor;
	QComboBox *cbBoxFrColor;
	QComboBox *cbBoxPlotAreaColor;
	QComboBox *cbBoxGridColor;
    QComboBox *cbBoxLegendAlign;
    QLineEdit *lEChartTitle;
    QLineEdit *lEXAxisName;
    QLineEdit *lEYAxisName;
    QDoubleSpinBox *spinBoxZoomFactor;
	
	QSpinBox *spinBoxXDecimal;
	QSpinBox *spinBoxYDecimal;
	QSpinBox *spinBoxXGridCount;
	QSpinBox *spinBoxYGridCount;
	QDoubleSpinBox *spinBoxXMin;
	QDoubleSpinBox *spinBoxXMax;
	QDoubleSpinBox *spinBoxYMin;
	QDoubleSpinBox *spinBoxYMax;
	QCheckBox *ckBoxXReverse;
	QCheckBox *ckBoxYReverse;

	QValueAxis *xAxis;    
	QValueAxis *yAxis;
    QLineSeries* currentSeries;
	QLineSeries* hoveredSeries;
};

#endif // CHARTVIEWEX_H
